package com.example.study.utils;

import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.http.ContentDisposition;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;

import java.io.File;
import java.util.ArrayList;

/**
 * @author kuotian
 */
public class FileUtil {

    public static String basePath() {
        return "/Users/kuotian/Desktop/demo/";
    }

    public static ArrayList<String> scanFile(String path) {
        System.out.println("apth === " + path);
        File dir = new File(path);
        ArrayList arrayList = new ArrayList();
        if (dir.isDirectory() == false) {
            return arrayList;
        }
        File[] list = dir.listFiles();
        for (int i = 0; i < list.length; i++) {
            File f = list[i];
            if (f.isFile()) {
                String fName = f.getName();
                if (fName.contains(".DS_Store")) {
                } else {
                    arrayList.add(f.getName());
                }
            }
        }
        return arrayList;
    }

    public static ResponseEntity<Resource> getFileStream(String path, MediaType fileType) {
        System.out.println("path == " + path);
        String filePath = "/Users/kuotian/Desktop/demo/" + path;
        String contentDisposition = ContentDisposition
                .builder("attachment")
                .filename(filePath)
                .build()
                .toString();
        return ResponseEntity.ok()
                .header(HttpHeaders.CONTENT_DISPOSITION, contentDisposition)
                .contentType(fileType)
                .body(new FileSystemResource(filePath));
    }


}
